package com.meihua.baoleyi.fragment.sickness;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseFragment;
import com.meihua.baoleyi.bean.DiskDesc;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.NetHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 专家建议
 * Created by liming  on 2017/10/19.
 */

public class ExpertAdviceFragment extends BaseFragment {

    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.tv_tip)
    TextView tvTip;
    @Bind(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    private static TreeMap<String, String> mTreeMap;
    private List<String> dataList = new ArrayList<>();
    private MyAdapter mAdapter;

    public static ExpertAdviceFragment newInstance(TreeMap<String, String> treeMap, int i) {
        ExpertAdviceFragment fragment = new ExpertAdviceFragment();
        Bundle mBundle = new Bundle();
        mBundle.putInt("ye_status", i);
        fragment.setArguments(mBundle);
        mTreeMap = treeMap;
        return fragment;
    }

    Context mcontext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.expert_advice_fragment, container, false);
        ButterKnife.bind(this, view);

        mAdapter = new MyAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        getServerData();
    }

    private void getServerData() {
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        int ye_status = getArguments().getInt("ye_status");
        treeMap.put("ye_status", ye_status + "");//类型
        treeMap.putAll(mTreeMap);
        NetHelper.post(APIServer.SUM_INFANTS, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                /*请求得到的数据*/
                DiskDesc bean = NetHelper.getBeanForJson(DiskDesc.class, s);
                if (bean == null) return;
                try {
                    if (bean.getCode().equals("100")) {
                        //请求成功
                        List<String> synthesise = bean.getData().getSynthesise();
                        if (synthesise == null) {
                            tvTip.setVisibility(View.VISIBLE);
                            return;
                        }
                        if (synthesise.size() > 0) {
                            dataList.clear();
                            dataList.addAll(synthesise);
                            mAdapter.notifyDataSetChanged();
                            tvTip.setVisibility(View.GONE);
                        } else {
                            tvTip.setVisibility(View.VISIBLE);
                        }
                    } else {
                        tvTip.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(mcontext,"服务器异常",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        public MyAdapter() {
            super();
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder) holder).displayData(dataList.get(position));
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sick_descript, parent, false);
            return new MyViewHolder(v);
        }

        @Override
        public int getItemCount() {
            return dataList.size() > 0 ? dataList.size() : 0;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private TextView name;

            public MyViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.tv_name);
            }

            public void displayData(String src) {
                if (name != null) {
                    name.setText(src);
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
